package performance.bat;

import java.io.*;

public class CreateBat {

	public static void main(String[] args) throws Exception {
		String pathroot = System.getProperty("user.dir");
		String targetroot = "D:/jdbctest2/";
		if(!pathroot.endsWith(File.separator)){
			pathroot = pathroot+File.separator;
		}
		boolean ifWindow = true;
		
		if(ifWindow){
			writeWindowsQuery(pathroot);
			writeWindowsInsert(pathroot);
			writeWindowsDelete(pathroot);
			clearDirectory(targetroot);
			copyFile(pathroot+"query.bat",targetroot+"query.bat");
			copyFile(pathroot+"insert.bat",targetroot+"insert.bat");
			copyFile(pathroot+"delete.bat",targetroot+"delete.bat");
			copyFile(pathroot+"target/classes",targetroot+"classes");
			copyFile(pathroot+"target/test-classes",targetroot+"test-classes");
			copyFile(pathroot+"lib",targetroot+"lib");
		}else{
			writeLinuxQuery(pathroot);
			writeLinuxInsert(pathroot);
			clearDirectory(targetroot);
			copyFile(pathroot+"query.sh",targetroot+"query.sh");
			copyFile(pathroot+"insert.sh",targetroot+"insert.sh");
			copyFile(pathroot+"target/classes",targetroot+"classes");
			copyFile(pathroot+"target/test-classes",targetroot+"test-classes");
			copyFile(pathroot+"lib",targetroot+"lib");
		}
		
	}
	
	public static void writeWindowsQuery(String pathroot){
		StringBuffer sb = new StringBuffer();
		sb.append("set main_class=performance.TpsQueryTest\r\n");
		sb.append("set java_options=-Xms128m -Xmx1024m\r\n");
		sb.append("set class_path=classes;test-classes;");

//		sb.append("java -Xms128m -Xmx1024m -classpath classes;test-classes");

		File dir = new File(pathroot+"lib");
		File[] list = dir.listFiles();
		for(File f:list){
		  if(f.getName().endsWith(".jar")){
			sb.append(System.getProperty("path.separator")).append("lib/").append(f.getName());	
		  }
		}
		sb.append("\r\njava %java_options% -classpath %class_path% %main_class%");
		sb.append("\r\n@pause");
		System.out.println(sb.toString());
		writeText(pathroot+"query.bat",sb.toString(),"utf-8");
	}
	
	public static void writeWindowsInsert(String pathroot){
		StringBuffer sb = new StringBuffer();
		sb.append("set main_class=performance.TpsExecuteTest\r\n");
		sb.append("set java_options=-Xms128m -Xmx1024m\r\n");
		sb.append("set class_path=classes;test-classes;");
		File dir = new File(pathroot+"lib");
		File[] list = dir.listFiles();
		for(File f:list){
		  if(f.getName().endsWith(".jar")){
			sb.append(System.getProperty("path.separator")).append("lib/").append(f.getName());	
		  }
		}
		sb.append("\r\njava %java_options% -classpath %class_path% %main_class%");
		sb.append("\r\n@pause");
		System.out.println(sb.toString());
		writeText(pathroot+"insert.bat",sb.toString(),"utf-8");
	}
	
	public static void writeWindowsDelete(String pathroot){
		StringBuffer sb = new StringBuffer();
		sb.append("set main_class=performance.TpsDeleteData\r\n");
		sb.append("set java_options=-Xms128m -Xmx1024m\r\n");
		sb.append("set class_path=classes;test-classes;");
		File dir = new File(pathroot+"lib");
		File[] list = dir.listFiles();
		for(File f:list){
		  if(f.getName().endsWith(".jar")){
			sb.append(System.getProperty("path.separator")).append("lib/").append(f.getName());	
		  }
		}
		sb.append("\r\njava %java_options% -classpath %class_path% %main_class%");
		sb.append("\r\n@pause");
		System.out.println(sb.toString());
		writeText(pathroot+"delete.bat",sb.toString(),"utf-8");
	}
	
	public static void writeLinuxQuery(String pathroot){
		StringBuffer sb = new StringBuffer();
		sb.append("java -classpath classes:test-classes");
		File dir = new File(pathroot+"lib");
		File[] list = dir.listFiles();
		for(File f:list){
		  if(f.getName().endsWith(".jar")){
			sb.append(":").append("lib/").append(f.getName());	
		  }
		}
		sb.append(" org.tinygroup.dbrouterjdbc3.jdbc.performance.TpsQueryTest $1 $2 $3 ");
		System.out.println(sb.toString());
		writeText(pathroot+"query.sh",sb.toString(),"utf-8");
	}
	
	public static void writeLinuxInsert(String pathroot){
		StringBuffer sb = new StringBuffer();
		sb.append("java -classpath classes:test-classes");
		File dir = new File(pathroot+"lib");
		File[] list = dir.listFiles();
		for(File f:list){
		  if(f.getName().endsWith(".jar")){
			sb.append(":").append("lib/").append(f.getName());	
		  }
		}
		sb.append(" org.tinygroup.dbrouterjdbc3.jdbc.performance.TpsExecuteTest $1 $2 $3 $4");
		System.out.println(sb.toString());
		writeText(pathroot+"insert.sh",sb.toString(),"utf-8");
	}
	
	/**
	 * 将文本内容写入指定的文件
	 * @param path
	 * @param b
	 * @return
	 */
	public static boolean writeText(String path,byte[] b){
		 FileOutputStream out = null;
	        try {
	            out = new FileOutputStream(path);
	            out.write(b);
	        }  catch (Exception e) {

	            e.printStackTrace();
	            return false;
	        } finally {
	            if (out != null) {
	                try {
	                    out.close();
	                } catch (IOException e) {

	                    e.printStackTrace();
	                }
	            }
	        }
	        return true;
	}
	
	/**
	 * 将文本内容写入指定的文件
	 * @param path
	 * @param text
	 * @param encode
	 * @return
	 */
	public static boolean writeText(String path, String text,String encode){
		 if(path==null || text==null || encode==null){
			 return false;
		 }
		byte[] b=null;
		try {
			b = text.getBytes(encode);
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
         return writeText(path,b);
	}
	
	public static boolean deleteDirectory(File f) throws Exception{
		boolean tag =false;
		
		try {
			File[] file =f.listFiles();
			if(file==null || file.length==0){
				f.delete();
			}else{
				for(int i=0;i<file.length;i++){
					
					if(file[i].isFile()){
						file[i].delete();
					}
					
					if(file[i].isDirectory()){
						deleteDirectory(file[i]);
					}
				}
				f.delete();
			}
			tag =true;
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		
		return tag;
	}
	
	public static void clearFile(String delpath)
	{
		String[] flist=new File(delpath).list();
		for(int i=0;i<flist.length;i++)
		{
			try {
				File file = new File(delpath+flist[i]);
				file.delete();
			}
			catch(Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * 清理目录
	 * @param delpath
	 * @return
	 * @throws Exception
	 */
	public static boolean clearDirectory(String delpath) throws Exception{
		File[] flist=new File(delpath).listFiles();
		if(flist!=null && flist.length>0){
			for(int i=0;i<flist.length;i++){
				deleteDirectory(flist[i]);
			}
		}
		return true;
	}
	
	public static boolean copyFile(String src, String dst) throws Exception {
		boolean tag =false;
		File srcfile=new File(src);
		File dstfile=new File(dst);
		if(srcfile.isFile()){
			tag =copySingleFile(srcfile,dstfile);
		}else{
			tag =copyDirectory(srcfile,dstfile);
		}
		return tag;
	}
	
	public static boolean copyDirectory(File src,File dst) throws Exception{
		try {
			dst.mkdirs();
			File[] file =src.listFiles();
			if(file!=null){
				//System.out.println("file size="+file.length);
				for (int i = 0; i < file.length; i++) {
					if (file[i].isFile()) {
						copySingleFile(file[i],dst);
					}
					
					if(file[i].isDirectory()){
						copyDirectory(file[i],new File(dst.getAbsoluteFile()+"/"+file[i].getName()));
					}
				}
			}
			return true;
		}
		catch(Exception e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public static boolean copySingleFile(File src ,File dst){
		boolean tag =false;
		FileInputStream in=null;
		FileOutputStream out=null;
		byte[] b = null;
		try {
			if(dst.isDirectory()){
				
				dst =new File(dst.getAbsoluteFile()+"/"+src.getName());
			}
			in =new FileInputStream(src.getAbsoluteFile());
			out =new FileOutputStream(dst.getAbsoluteFile());
			b=new byte[8192];
			int byteread=0;
			
			while((byteread=in.read(b))!=-1){
				out.write(b, 0, byteread);
			}
			//out.flush();
			tag =true;
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return tag;
		} finally{
			if(in!=null){
				try {
					in.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				in=null;
			}
			
			if(out!=null){
				try {
					out.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				out=null;
			}
			
			b=null;
		}
		return tag;
	}

}
